package com.hongyi.system.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hongyi.common.excel.utils.ExcelUtil;
import com.hongyi.common.framework.constant.UserConstans;
import com.hongyi.common.framework.domain.JsonResult;
import com.hongyi.common.framework.validate.AddGroup;
import com.hongyi.common.framework.validate.EditGroup;
import com.hongyi.common.log.annotation.Log;
import com.hongyi.common.log.enums.BusinessType;
import com.hongyi.common.mybatis.page.PageData;
import com.hongyi.common.mybatis.page.PageQuery;
import com.hongyi.common.web.controller.BaseController;
import com.hongyi.system.domain.dto.SysConfigDto;
import com.hongyi.system.domain.vo.SysConfigVo;
import com.hongyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 参数配置表
*
* @author DingHao
*/
@ApiSort(value = 12)
@Api(tags = "参数配置")
@Validated
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {

    @Autowired
    private ISysConfigService sysConfigService;

    /**
    * 获取参数配置分页列表
    *
    * @param sysConfigDto 参数配置数据
    * @param pageQuery 分页数据
    * @return 参数配置查询结果
    */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取参数配置列表")
    @SaCheckPermission("system:config:list")
    @GetMapping(value = "/list")
    public PageData<SysConfigVo> list(SysConfigDto sysConfigDto, PageQuery pageQuery) {
        return sysConfigService.queryConfigPageList(sysConfigDto,pageQuery);
    }

    /**
     * 参数导出
     *
     * @param sysConfigDto 参数对象
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "参数管理导出")
    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:config:export")
    @PostMapping(value = "/exportByStream", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportStream(SysConfigDto sysConfigDto, HttpServletResponse response) {
        List<SysConfigVo> sysConfigVos = sysConfigService.selectConfigList(sysConfigDto);
        ExcelUtil.exportExcel(sysConfigVos, "参数数据", SysConfigVo.class, response);
    }

    /**
     * 根据参数编号获取详细信息
     *
     * @param configId 主键
     * @return 参数配置查询结果
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据编号获取详细信息")
    @ApiImplicitParam(name = "configId", value = "编号", paramType = "path", dataTypeClass = Long.class, required = true)
    @SaCheckPermission("system:config:query")
    @GetMapping(value = "/{configId}")
    public JsonResult<SysConfigVo> getInfo(@PathVariable Long configId) {
        return JsonResult.ok(sysConfigService.queryConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     *
     * @param configKey 参数键名
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "根据参数键名查询参数值")
    @ApiImplicitParam(name = "configKey", value = "参数键名", paramType = "path", dataTypeClass = Long.class, required = true)
    @GetMapping(value = "/configKey/{configKey}")
    public JsonResult<String> getConfigKey(@PathVariable String configKey) {
        return JsonResult.ok(sysConfigService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     *
     * @param sysConfigDto 参数配置数据
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "新增参数配置")
    @SaCheckPermission("system:config:add")
    @Log(title = "新增参数配置", businessType = BusinessType.INSERT)
    @PostMapping
    public JsonResult<String> add(@Validated(AddGroup.class) @RequestBody SysConfigDto sysConfigDto) {
        if (UserConstans.NOT_UNIQUE.equals(sysConfigService.checkConfigKeyUnique(sysConfigDto))) {
            return JsonResult.fail("新增参数'" + sysConfigDto.getConfigName() + "'失败，参数键名已存在");
        }
        sysConfigService.insertConfig(sysConfigDto);
        return JsonResult.ok();
    }

    /**
     * 修改参数配置
     *
     * @param sysConfigDto 参数配置数据
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "修改参数配置")
    @SaCheckPermission("system:config:edit")
    @Log(title = "修改参数配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public JsonResult<Void> edit(@Validated(EditGroup.class) @RequestBody SysConfigDto sysConfigDto) {
        if (UserConstans.NOT_UNIQUE.equals(sysConfigService.checkConfigKeyUnique(sysConfigDto))) {
            return JsonResult.fail("修改参数'" + sysConfigDto.getConfigName() + "'失败，参数键名已存在");
        }
        sysConfigService.updateConfig(sysConfigDto);
        return JsonResult.ok();
    }

    /**
     * 删除参数配置
     *
     * @param configIds 主键数组
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "删除参数配置")
    @SaCheckPermission("system:config:remove")
    @ApiImplicitParam(name = "configIds", value = "参数ID数据", paramType = "path", dataTypeClass = Long[].class, required = true)
    @Log(title = "删除参数配置", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{configIds}")
    public JsonResult<Void> remove(@PathVariable Long[] configIds) {
        sysConfigService.deleteConfigByIds(configIds);
        return JsonResult.ok();
    }

    /**
     * 清空缓存
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "清空缓存")
    @SaCheckPermission("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping(value = "/clearCache")
    public JsonResult<String> clearCache() {
        sysConfigService.resetConfigCache();
        return JsonResult.ok();
    }


}
